import { dialog } from 'electron'
import fs from 'fs'
import path from 'path'

export const openDirectoryDialog = async () => {
  const result = await dialog.showOpenDialog({
    properties: ['openDirectory'],
    title: '选择包含音频文件的目录',
    buttonLabel: '选择'
  })

  return result.canceled ? null : result.filePaths[0]
}

export const getFileDataByFilePath = async (filePath) => {
  // 检查文件是否存在
  if (!fs.existsSync(filePath)) {
    console.log('文件不存在！')
    return
  }
  try {
    // 读取文件并转为 Base64
    const data = await fs.promises.readFile(filePath)
    const base64Data = `data:audio/mp3;base64,${data.toString('base64')}`
    return { success: true, base64Data: base64Data }
  } catch (error) {
    return { success: false, error: error.message }
  }
}
export const getFileBufByFilePath = async (filePath) => {
  // 检查文件是否存在
  if (!fs.existsSync(filePath)) {
    console.log('文件不存在！')
    return
  }
  try {
    // 读取文件并转为 Base64
    const data = await fs.promises.readFile(filePath)
    return { success: true, buffer: data }
  } catch (error) {
    return { success: false, error: error.message }
  }
}
